
/**
 * Created by apple on 16/6/26.
 */
angular.module("starter.directive", ['ionic'])
  .directive('devinLoading', function () {
    return {
      restrict: 'AE',
      transclude: true,
      replace: true,
      template: '<div><ion-spinner icon="{{spinner}}" class="spinner-calm"></ion-spinner><br/>' +
      '<span>{{content}}</span></div>',
      scope: {
        spinner: '@spinner',
        content: '@content'
      }
    };
  })

  .directive('devinLoadingOver', function () {
    return {
      restrict: 'AE',
      transclude: true,
      replace: true,
      template: '<div><i class="icon {{spinner}}"></i><br/>' +
      '<span>{{content}}</span></div>',
      scope: {
        spinner: '@spinner',
        content: '@content'
      }
    };
  })

  .directive('devinHideTabs', function($rootScope,$timeout) {
    return {
      restrict: 'AE',
      link: function(scope, element, attributes) {
        scope.$on('$ionicView.beforeEnter', function() {
          scope.$watch(attributes.devinHideTabs, function(value){
            //$timeout(function(){
              $rootScope.devinHideTabs = value;
            //},1);
            //console.log('devinHideTabs:'+value);
          });
        });

        scope.$on('$ionicView.beforeLeave', function() {
          $rootScope.devinHideTabs = false;
        });
      }
    };
  })

  .directive('choiceCity', ['$timeout', function($timeout) {
    return {
      restrict: 'AE',
      scope: {
        //trigger: '@focusMe'

      },
      link: function(scope, element) {

      }
    }
  }])

  .directive('stringtonumber', function() {
    return {
      require: 'ngModel',
      link: function(scope, element, attrs, ngModel) {
        ngModel.$parsers.push(function(value) {
          if (value === null){
            return '';
          }else {
            return '' + value;
          }
        });
        ngModel.$formatters.push(function(value) {
          try{
            if(typeof (value) == 'undefined' || value == '') {
              return NaN;
            }
            if(parseFloat(value) + '' == value){
              return parseFloat(value);
            }else{
              return value + '';
            }
          }catch (e){
            console.log(e);
            return value+'' ;
          }
        });
      }
    };
  })

//  .directive('focusMe', ['$timeout', function($timeout) {
//  return {
//    restrict: 'AE',
//    scope: { trigger: '@focusMe' },
//    link: function(scope, element) {
//      scope.$watch('trigger', function(value) {
//        //console.log('true' == value );
//        if(value == "true") {
//          $timeout(function() {
//            //element[0].focus();
//          });
//        }
//      });
//    }
//  };
//}]);
